package com.taptech.doufu.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.MoreNovelBean;
import com.taptech.doufu.bean.TagBean;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.StringUtil;
import com.taptech.doufu.ui.view.choice.ChoiceUtils;

import java.util.ArrayList;
import java.util.List;

public class NovelVerticalCoverAdapter extends BaseAdapter
{


    private Context mContext;
    private List<MoreNovelBean> mList = new ArrayList<>();


    public NovelVerticalCoverAdapter(Context context) {
        mContext = context;
    }

    public void addAll(List<MoreNovelBean> list){
        if (list != null) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void setData(List<MoreNovelBean> list) {
        if (list != null) {
            mList = list;
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public MoreNovelBean getItem(int position) {
        if (mList.size() > position) {
            return mList.get(position);
        }else{
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MoreNovelBean bean = getItem(position);
        if (bean == null) {
            return new View(mContext);
        }
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.more_novel_item, null);
            holder = new ViewHolder();
            holder.title =  convertView.findViewById(R.id.title);
            holder.ivPic =  convertView.findViewById(R.id.ivPic);
            holder.tvTag1 =  convertView.findViewById(R.id.tvTag1);
            holder.tvTag2 =  convertView.findViewById(R.id.tvTag2);
            holder.tvDesc =  convertView.findViewById(R.id.tvDesc);
            holder.tvAuthorDesc =  convertView.findViewById(R.id.tvAuthorDesc);
            holder.lay = convertView.findViewById(R.id.lay);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == 0) {
            holder.lay.setPadding(0, ScreenUtil.dip2px(mContext,16),0,ScreenUtil.dip2px(mContext,16));
        }else{
            holder.lay.setPadding(0, 0,0,ScreenUtil.dip2px(mContext,16));
        }
        if (!TextUtils.isEmpty(bean.getTitle())) {
            holder.title.setText(bean.getTitle());
        }
        if (!TextUtils.isEmpty(bean.getBrief())) {
            holder.tvDesc.setText(StringUtil.filterLineFeed(bean.getBrief()));
        }else if(!TextUtils.isEmpty(bean.getDescription())){
            holder.tvDesc.setText(StringUtil.filterLineFeed(bean.getDescription()));
        }else if(!TextUtils.isEmpty(bean.getDes())){
            holder.tvDesc.setText(StringUtil.filterLineFeed(bean.getDes()));
        }
        if (!TextUtils.isEmpty(bean.getCover())) {
            ChoiceUtils.displayImage(mContext,bean.getCover(),holder.ivPic);
        }else if (bean.getImages() != null && bean.getImages().length > 0) {
            ChoiceUtils.displayImage(mContext,bean.getImages()[0].getImgUrl(),holder.ivPic);
        }
        if (bean.getUser() != null) {
            if (!TextUtils.isEmpty(bean.getUser().getNickname())) {
                holder.tvAuthorDesc.setText(bean.getUser().getNickname());
            }else if (!TextUtils.isEmpty(bean.getUser().getName())) {
                holder.tvAuthorDesc.setText(bean.getUser().getName());
            }
        }
        if (bean.getTags() != null) {
            if (bean.getTags().size() > 0) {
                final TagBean tag = bean.getTags().get(0);
                if (tag != null) {
                    holder.tvTag1.setText(tag.getName());
                    holder.tvTag1.setVisibility(View.VISIBLE);
                    holder.tvTag1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ChoiceUtils.jumpTagDetail(mContext,tag.getName());
                        }
                    });
                }else{
                    holder.tvTag1.setVisibility(View.GONE);
                }
            }else{
                holder.tvTag1.setVisibility(View.GONE);
            }
            if (bean.getTags().size() > 1) {
                final TagBean tag = bean.getTags().get(1);
                if (tag != null) {
                    holder.tvTag2.setText(tag.getName());
                    holder.tvTag2.setVisibility(View.VISIBLE);
                    holder.tvTag2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ChoiceUtils.jumpTagDetail(mContext,tag.getName());
                        }
                    });
                }else{
                    holder.tvTag2.setVisibility(View.GONE);
                }
            }else{
                holder.tvTag2.setVisibility(View.GONE);
            }
        }
        holder.lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChoiceUtils.jumpToNovelDetail(mContext,bean.getId());
            }
        });
        return convertView;
    }


    public class ViewHolder{
        TextView title;
        ImageView ivPic;
        TextView tvTag1;
        TextView tvTag2;
        TextView tvDesc;
        TextView tvAuthorDesc;
        View lay;
    }
}
